/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.baumi.tankwars.entities;

import it.baumi.tankwars.Game;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Michael Baumgartner
 */
public class ShotEntity extends MoveableEntity2D {

    Game game;
    double startX;
    double startY;
    double degree;
    int popCount;

    public ShotEntity(Game game, double x, double y, double width, double height, double speed, double degree) {
        super(x, y, width, height, speed);
        this.game = game;
        startX = x;
        startY = y;
        this.degree = degree;
        popCount = 0;
    }

    @Override
    public void setUp() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void draw() {

        x += speed * Math.cos(degree);
        y += speed * Math.sin(degree);

        if (x >= game.getWidth() - width / 2) {
            x = game.getWidth() - width / 2;
            destroy();
        }
        if (x <= 0 + width / 2) {
            x = width / 2;
            destroy();
        }
        if (y <= 0 + height / 2) {
            y = height / 2;
            destroy();
        }
        if (y >= game.getHeight() - height / 2) {
            y = game.getHeight() - height / 2;
            destroy();
        }

        GL11.glBegin(GL11.GL_QUADS);
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glVertex2d(x - width / 2, y - height / 2);
        GL11.glVertex2d(x - width / 2, y + height / 2);
        GL11.glVertex2d(x + width / 2, y + height / 2);
        GL11.glVertex2d(x + width / 2, y - height / 2);
        GL11.glEnd();
    }

    @Override
    public void collideWith(BlockEntity2D otherEntity) {
        if (otherEntity instanceof TankEntity || otherEntity instanceof ShotEntity) {
            this.destroy();
            otherEntity.destroy();
        }

        if (otherEntity instanceof WallEntity) {
            if (popCount < 1) {
                popCount++;

                double winkel = Math.toDegrees(degree);

                double north = this.getyMax() - otherEntity.getyMin();
                double east = otherEntity.getxMax() - this.getxMin();
                double south = otherEntity.getyMax() - this.getyMin();
                double west = this.getxMax() - otherEntity.getxMin();

                double min = Math.min(Math.min(Math.min(north, east), west), south);
                if (min == north ||min==south) {
                    winkel = - winkel;
                   
                } else if (min == east || min == west) {
                    winkel = 180 - winkel;
                    
                } 
                degree = Math.toRadians(winkel);
            } else {
                this.destroy();
            }

        }
    }

    @Override
    public void update() {
        draw();
    }

}
